#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void min(int& a, int& b)
{
	int c;
	if (a < b)
	{
		c = b;
		b = a;
		a = b;
	}
}
void maopao(vector<int>&A,int k)
{
	for (int j = 0; j < k+1; j++)
	{
		for (int i = 0; i < A.size() -1-j; i++)
		{
			min(A[i], A[i + 1]);
		}
	}
}
int main() {
	//vector<vector<int>>V;
	
	int m, n, i, j,x;
	cin >> n;
	int** V = new int*[n];
	//长度
	int* Vileng = new int [n];
	//int** V = new int*[n];
	for (i = 0; i<n; i ++)
	{
		cin >> m;
		Vileng[i] = m;
		V[i] = new int[m];
		for (j = 0; j<m; j ++)
		{
			cin >> x;
			V[i][j] = x;
			//cin >> V[i][j];
		}
	}
	//for (i = 0; i<n; i++)
	//{
	//	
	//	for (j = 0; j<m; j ++)
	//	{
	//		
	//		cout<<V[i][j];
	//	}
	//}
	int p;
	//p轮询问
	//给出
	int a;//合并a个数组
	int b;//
	int k;
	int g;
	
	cin >> p;
	vector<int>VV;
	for (i = 0; i < p; i++)
	{
		vector<int>VV;
		cin >> a;
		//V[i] = new int[m];
		for (j = 0; j < a; j++)
		{
			cin >> b;

			b--;
			for (g = 0; g <Vileng[b] ; g++)
			{
				VV.push_back(V[b][g]);
				//第b个数组加入合并
			}
			//第b个数组加入合并
		}
		cin >> k;
		//合并后新数组的第k小是
		sort(VV.begin(), VV.end());
		//maopao(VV, k);
		//cout << maopao(VV,k ) << endl;
		cout << VV[k-1]<<endl;
	}
}
